import { HorizolScroll, BellOutlined, isArray, isEmpty } from 'esy-ui';
import { useEffect, useState } from 'react';
import { postNotices } from '@/services';

const list = [
  { text: '这是一个水平滚动组件1~~~', url: 'https://www.baidu.com', id: 1 },
  { text: '这是一个水平滚动组件2~~~', url: 'https://taro.jd.com/home/in.html', id: 2 },
  { text: '这是一个水平滚动组件4~~~', url: 'https://www.baidu.com', id: 4 },
];

function Notice() {
  const [nots, setNots] = useState([]);
  useEffect(() => {
    postNotices({})
      .then((res: any) => {
        if (isArray(res?.Data)) {
          setNots(res.Data.map((it) => ({ ...it, text: it.Title })));
        } else {
          throw new Error('error');
        }
      })
      .catch(() => {
        setNots(list);
      });
  }, []);

  return (
    <div className='flex items-center mb-0.5 h-7'>
      {isArray(nots) && !isEmpty(nots) ? (
        <>
          <BellOutlined className='text-primary text-lg mr-0.5' />
          <HorizolScroll
            // onClick={(info) => {
            //   window.open(info.url);
            // }}
            list={nots}
            speedRate={0.05}
          />
        </>
      ) : null}
    </div>
  );
}

export default Notice;
